package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.OrganizationDepartmentPrincipal;
import com.xmy.cultivate.entity.TeacherPoint;
import com.xmy.cultivate.service.IOrganizationDepartmentPrincipalService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.CommonUtil;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 部门负责人详情 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2025-04-28
 */
@RestController
@RequestMapping("/api/admin/organizationDepartmentPrincipal")
public class OrganizationDepartmentPrincipalController extends BaseController{

    @Autowired
    IOrganizationDepartmentPrincipalService iOrganizationDepartmentPrincipalService;

    /**
     * 业绩数据列表
     * @param pageNum
     * @param pageSize
     * @param export
     * @param yearPart
     * @param quarterNum
     * @param staffName
     * @param departmentId
     * @param schoolId
     * @param subjectId
     * @param type
     * @return
     * @throws IOException
     */
    @GetMapping("/findAll")
    public Result findAll(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                              @RequestParam(value = "export", defaultValue = "0", required = false) Integer export,
                              @RequestParam(value = "yearPart", required = false) String yearPart,  //年份
                              @RequestParam(value = "quarterNum", required = false) String quarterNum, //学期
                              @RequestParam(value = "staffName", required = false) String staffName, //姓名
                              @RequestParam(value = "departmentId", required = false) String departmentId,     //部门ID
                              @RequestParam(value = "schoolId", required = false) String schoolId,     //学校ID
                              @RequestParam(value = "fiducialSetId", required = false) String fiducialSetId,     //项目id
                              @RequestParam(value = "type", required = false) String type,    //0绿色 1黄色 2红色
                              @RequestParam(value = "isDescOrAsc", required = false) String isDescOrAsc   //0正序 1倒序
    ) throws IOException {
        IPage<OrganizationDepartmentPrincipal> iPage = new Page<>(pageNum, pageSize);
        iOrganizationDepartmentPrincipalService.findAll(iPage,yearPart,quarterNum,staffName,departmentId,schoolId,fiducialSetId,type, isDescOrAsc);

        if (export == 1) {
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet("业绩数据");
            CellStyle headerCellStyle = workbook.createCellStyle();
            Font headerFont = workbook.createFont();
            headerFont.setFontHeightInPoints((short) 11);
            headerCellStyle.setFont(headerFont);
            headerCellStyle.setAlignment(HorizontalAlignment.CENTER);
            headerCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            Row headerRow = sheet.createRow(0);
            sheet.createFreezePane(0, 1);// 冻结首行

            CellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setFont(headerFont);
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

            List<String> headList = new ArrayList<>();
            headList.add("校区");
            headList.add("部门");
            headList.add("姓名");
            headList.add("绿");
            headList.add("黄");
            headList.add("红");
            for (int i = 0; i < headList.size(); i++) {
                Cell cell = headerRow.createCell(i);
                cell.setCellValue(headList.get(i));
                cell.setCellStyle(headerCellStyle);
            }
            int rowIndex = 1;// 数据行起始索引
            for (OrganizationDepartmentPrincipal organizationDepartmentPrincipal : iPage.getRecords()) {
                Row row = sheet.createRow(rowIndex++);

                Cell cell1 = row.createCell(0);
                cell1.setCellValue(organizationDepartmentPrincipal.getSchoolName());
                sheet.setColumnWidth(0, 15 * 256);
                cell1.setCellStyle(cellStyle);

                Cell cell2 = row.createCell(1);
                cell2.setCellValue(organizationDepartmentPrincipal.getDepartmentName());
                sheet.setColumnWidth(1, 15 * 256);
                cell2.setCellStyle(cellStyle);

                Cell cell3 = row.createCell(2);
                cell3.setCellValue(organizationDepartmentPrincipal.getStaffName());
                sheet.setColumnWidth(2, 15 * 256);
                cell3.setCellStyle(cellStyle);

                Cell cell4 = row.createCell(3);
                cell4.setCellValue(organizationDepartmentPrincipal.getGreenCount());
                sheet.setColumnWidth(3, 15 * 256);
                cell4.setCellStyle(cellStyle);


                Cell cell5 = row.createCell(4);
                cell5.setCellValue(organizationDepartmentPrincipal.getYellowCount());
                sheet.setColumnWidth(4, 15 * 256);
                cell5.setCellStyle(cellStyle);

                Cell cell6 = row.createCell(5);
                cell6.setCellValue(organizationDepartmentPrincipal.getRedCount());
                sheet.setColumnWidth(5, 15 * 256);
                cell6.setCellStyle(cellStyle);

            }

            String fileName = "业绩数据导出" + ".xlsx";
            String encodedFileName = URLEncoder.encode(fileName, "UTF-8");

            response.setContentType("application/zip");
            response.setHeader("Content-Disposition", "attachment; filename=" + encodedFileName);
            // 获取响应输出流
            OutputStream outputStream = response.getOutputStream();
            // 将工作簿写入输出流
            workbook.write(outputStream);
            // 关闭工作簿
            workbook.close();
            // 刷新输出流
            outputStream.flush();
            //关闭输出流
            outputStream.close();

        }

        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }

    @GetMapping("/details")
    public Result details(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                          @RequestParam(value = "export", defaultValue = "0", required = false) Integer export,
                          @RequestParam(value = "yearPart", required = false) String yearPart,  //年份
                          @RequestParam(value = "quarterNum", required = false) String quarterNum, //学期
                          @RequestParam(value = "staffId", required = false) String staffId,
                          @RequestParam(value = "departmentId", required = false) String departmentId,     //部门ID
                          @RequestParam(value = "schoolId", required = false) String schoolId,     //学校ID
                          @RequestParam(value = "type", required = false) String type,   //0绿色 1黄色 2红色
                          @RequestParam(value = "lessonNum", required = false) String lessonNum,
                          @RequestParam(value = "fiducialSetId", required = false) String fiducialSetId
    ) throws IOException {

        IPage<OrganizationDepartmentPrincipal> iPage = new Page<>(pageNum, pageSize);
        iOrganizationDepartmentPrincipalService.details(iPage, yearPart, quarterNum, staffId, departmentId, schoolId, type , lessonNum, fiducialSetId);

        if (export == 1) {
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet("业绩数据详情");
            CellStyle headerCellStyle = workbook.createCellStyle();
            Font headerFont = workbook.createFont();
            headerFont.setFontHeightInPoints((short) 11);
            headerCellStyle.setFont(headerFont);
            headerCellStyle.setAlignment(HorizontalAlignment.CENTER);
            headerCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            Row headerRow = sheet.createRow(0);
            sheet.createFreezePane(0, 1);// 冻结首行

            CellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setFont(headerFont);
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

            List<String> headList = new ArrayList<>();
            headList.add("姓名");
            headList.add("项目");
            headList.add("课次");
            headList.add("基准值");
            headList.add("数值");
            for (int i = 0; i < headList.size(); i++) {
                Cell cell = headerRow.createCell(i);
                cell.setCellValue(headList.get(i));
                cell.setCellStyle(headerCellStyle);
            }
            int rowIndex = 1;// 数据行起始索引
            for (OrganizationDepartmentPrincipal organizationDepartmentPrincipal : iPage.getRecords()) {
                Row row = sheet.createRow(rowIndex++);

                Cell cell1 = row.createCell(0);
                cell1.setCellValue(organizationDepartmentPrincipal.getStaffName());
                sheet.setColumnWidth(0, 15 * 256);
                cell1.setCellStyle(cellStyle);

                Cell cell2 = row.createCell(1);
                cell2.setCellValue(organizationDepartmentPrincipal.getProjectsName());
                sheet.setColumnWidth(1, 15 * 256);
                cell2.setCellStyle(cellStyle);

                Cell cell3 = row.createCell(2);
                cell3.setCellValue(organizationDepartmentPrincipal.getLessonNum());
                sheet.setColumnWidth(2, 15 * 256);
                cell3.setCellStyle(cellStyle);

                Cell cell4 = row.createCell(3);
                cell4.setCellValue(organizationDepartmentPrincipal.getReferenceValue());
                sheet.setColumnWidth(3, 15 * 256);
                cell4.setCellStyle(cellStyle);

                Cell cell5 = row.createCell(4);
                cell5.setCellValue(organizationDepartmentPrincipal.getRadeNum());
                sheet.setColumnWidth(4, 15 * 256);
                cell5.setCellStyle(cellStyle);

            }
            String fileName = "业绩数据详情导出" + ".xlsx";
            String encodedFileName = URLEncoder.encode(fileName, "UTF-8");

            response.setContentType("application/zip");
            response.setHeader("Content-Disposition", "attachment; filename=" + encodedFileName);
            // 获取响应输出流
            OutputStream outputStream = response.getOutputStream();
            // 将工作簿写入输出流
            workbook.write(outputStream);
            // 关闭工作簿
            workbook.close();
            // 刷新输出流
            outputStream.flush();
            //关闭输出流
            outputStream.close();

        }
        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }


}
